package com.qdxwx.data.service.advert;


import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.advert.AdvertMapper;
import com.qdxwx.models.advert.Advert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 广告图service 实现
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class AdvertServiceImpl implements AdvertService {

	@Autowired
	private AdvertMapper advertMapper;

	public int insert(Advert advert) {
		//CacheUtils.remove(CacheConstants.ADVERT_LIST);
		advert.setUpdateTime(LocalDateTime.now());
		return advertMapper.insertImage(advert);
	}

	@Override
	public List<Advert> getList(Advert advert, PageRequest page) {
		return advertMapper.selectList(advert, page);
	}

	@Override
	public List<Advert> getList(Advert advert, int limit) {
		return getList(advert, new PageRequest(1, limit));
	}

	@Override
	public List<Advert> getList(int channelId, int typeId, int limit) {
		Advert advert = new Advert();
		advert.setChannelId(channelId);
		advert.setTypeId(typeId);
		return getList(advert, new PageRequest(1, limit));
	}

	@Override
	public long count(Advert advert) {
		return advertMapper.selectCount(advert);
	}

	@Override
	public Advert get(int id) {
		if (id < 1) return null;
		return advertMapper.selectById(id);
	}

	@Override
	public int delete(int id) {
		if(id < 1) return 0;
		int row = advertMapper.deleteById(id);
		//if(row > 0) CacheUtils.remove(CacheConstants.ADVERT_LIST);
		return row;
	}

	@Override
	public int update(Advert advert) {
		advert.setUpdateTime(LocalDateTime.now());
		int row = advertMapper.updateImage(advert);
		//if (row > 0) CacheUtils.remove(CacheConstants.ADVERT_LIST);
		return row;
	}
}
